#pragma once
#include <pthread.h>
#include <string>

typedef void *(*func_t)(void *);

struct ThreadData
{  
    int _number;
    void *_objThis; 
    std::string _name;

    ThreadData()=default;

    ThreadData(int number,void *objThis,const std::string &name)
        :_number(number)
        ,_objThis(objThis)
        ,_name(name)
    {}
};

class Thread
{
private:
    pthread_t _tid; // 线程id
    func_t _func; // 线程回调函数
    ThreadData _tData; // 线程对应的数据
public:
    Thread(int number,func_t func,void *objThis)
    {
        _func=func;
        _tData._number=number;
        _tData._objThis=objThis;
        _tData._name=std::string("thread-")+std::to_string(number);
    }
    ~Thread()
    {
        Cancel();
        Join();
    }

    void Create()
    {
        pthread_create(&_tid,nullptr,_func,&_tData);
    }

    void Join()
    {
        pthread_join(_tid,nullptr);
    }

    void Cancel()
    {
        pthread_cancel(_tid);
    }

    std::string &GetName()
    {
        return _tData._name;
    } 

};